//  Android Carpool System
//  2011-2011
//  Author: Jinhuan Wang
//  name:  Signin.java
//  description: validate the username and password 
//$history: /carpoolservice/scr/com/carpool/service/Signin.java$
/*************************************************/
package com.carpool.service;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class Signin extends HttpServlet {
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		DBOperation DBO = new DBOperation();
		int uid = 0;
		String cellphone = null;
		String uname=null;
		String password=null;
		String usertype=null;
		List rpl=null;
		try {
		InputStream is = request.getInputStream();
		DataInputStream dis = new DataInputStream(is);
		uname = dis.readUTF();
		password = dis.readUTF();
		}
		catch(Exception e){}
		
		try {
			rpl = DBO.login(uname, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (rpl != null) {
			uid = (Integer) rpl.get(0);
			cellphone =(String)  rpl.get(1);
			usertype=((Boolean)rpl.get(2)?"driver":"passager");
		}
		
		DataOutputStream dos = new DataOutputStream(response.getOutputStream());
		dos.writeInt(uid);
		dos.writeUTF(cellphone);
		dos.writeUTF(usertype);
		dos.flush();
		dos.close();

	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException {doPost( request, response);}

}
